package com.nl.controller.xuyi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.web.service.SmsInterfacesService;
import com.nl.controller.web.util.StringUtil;
import com.nl.controller.xuyi.bean.XuYiCheckInfoBo;
import com.nl.controller.xuyi.bean.XuYiPointInfo;
import com.nl.controller.xuyi.mapper.XuYiCheckMapper;
import com.nl.controller.xuyi.mapper.XuYiPointMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc
 * @return null
 * @author ll
 * @date 2020/9/27 11:08
 */
@Service
@Slf4j
public class XuYiPointService {
    @Autowired
    private XuYiCheckMapper xuYiCheckMapper;
    @Autowired
    private XuYiPointMapper xuYiPointMapper;

    public PageInfo<XuYiPointInfo> findPointNameList(int page, int limit, String regionName, String alreadyRelate) {
        List<XuYiPointInfo> pointNameList = new ArrayList<XuYiPointInfo>();
        PageHelper.startPage(page, limit);
        String[] alreadyRelateList = null;
        List<String> alreadyRelateList2 = null;
        if(""!=alreadyRelate&&null!=alreadyRelate){
            alreadyRelateList = alreadyRelate.split(",");
            alreadyRelateList2 = new ArrayList<String>();
            for(String region:alreadyRelateList){
                alreadyRelateList2.add(region);
            }
        }
        pointNameList= xuYiPointMapper.findPointNameList(regionName,alreadyRelateList2);
        PageInfo<XuYiPointInfo> pageInfo = new PageInfo<XuYiPointInfo>(pointNameList);
        return pageInfo;
    }

    public int pointInfoAdd(String pointName, String gpsLngLats,String remark,String operatorTel) {
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        int insertPointResult = 0;
        int insertPointCount = 0;

        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();

        String[] gpsLngLatArr = gpsLngLats.split("\\|");
        String gpsLng;
        String gpsLat;
        String bdLng = "";
        String bdLat = "";
        String bdLngLats = "";
        int insertCheckResult =0;
        String pointId;
        try{
            if(null!=gpsLngLatArr&&gpsLngLatArr.length>0){
                for(String gpsLngLat : gpsLngLatArr){
                    gpsLng = gpsLngLat.split(",")[0];
                    gpsLat = gpsLngLat.split(",")[1];
                    String lngLat = SmsInterfacesService.GPSToBd(gpsLng, gpsLat);

                    bdLng = lngLat.split(",")[0];
                    bdLat = lngLat.split(",")[1];
                    if(StringUtil.isNull(bdLngLats)){
                        bdLngLats = bdLng + "," + bdLat;
                    }else{
                        bdLngLats = bdLngLats + "|" +bdLng + "," + bdLat;
                    }
                }

                pointId = xuYiPointMapper.findPointSeq();
                //直接入表
                /*xuYiPointInfo.setId(pointId);
                xuYiPointInfo.setName(pointName);
                xuYiPointInfo.setGpsLngLats(gpsLngLats);
                xuYiPointInfo.setBdLngLats(bdLngLats);
                xuYiPointInfo.setState("1");
                xuYiPointInfo.setCheckState("2");
                xuYiPointInfo.setOperatorTel(operatorTel);
                insertPointResult = xuYiPointMapper.addPointInfo(xuYiPointInfo);*/

                //入审核表
                xuYiCheckInfoBo.setPointId(pointId);
                xuYiCheckInfoBo.setPointName(pointName);
                xuYiCheckInfoBo.setGpsItdu(gpsLngLats);
                xuYiCheckInfoBo.setBdItdu(bdLngLats);
                xuYiCheckInfoBo.setRemark(remark);
                xuYiCheckInfoBo.setCheckState("0");
                xuYiCheckInfoBo.setType("1");
                xuYiCheckInfoBo.setOperator(operatorTel);
                insertPointResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);
                if(insertPointResult>0){
                    insertPointCount++;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("新增点位信息发生异常:"+e.toString());
        }

        return  insertPointCount;
    }

    public List<XuYiPointInfo> findPointNameList2(String pointNameText) {
        List<String> list = new ArrayList<>();
        List<XuYiPointInfo> xuYiPointInfoList = xuYiPointMapper.findPointNameList(pointNameText,list);
        return xuYiPointInfoList;

    }

    public PageInfo<XuYiPointInfo> findPointInfoList(int page, int limit, String pointNameText) {
        PageHelper.startPage(page, limit);
        List<XuYiPointInfo> pointInfoList = new ArrayList<XuYiPointInfo>();
        if("all".equals(pointNameText)){
            pointNameText = "";
        }

        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        xuYiPointInfo.setName(pointNameText);
        pointInfoList = xuYiPointMapper.findPointInfo(xuYiPointInfo);
        PageInfo<XuYiPointInfo> pageInfo = new PageInfo<XuYiPointInfo>(pointInfoList);
        return pageInfo;
    }

    public int judgeIsRepeat(String pointName) {
        List<XuYiPointInfo> xuYiPointInfos;
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        xuYiPointInfo.setName(pointName);
        xuYiPointInfos = xuYiPointMapper.findPointInfo(xuYiPointInfo);
        if(!CollectionUtils.isEmpty(xuYiPointInfos)){
            xuYiPointInfo = xuYiPointInfos.get(0);
            return 1;
        }else{
            return 0;
        }
    }

    public XuYiPointInfo findPointInfoByName(String pointName) {
        List<XuYiPointInfo> xuYiPointInfos;
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        xuYiPointInfo.setName(pointName);
        xuYiPointInfos = xuYiPointMapper.findPointInfo(xuYiPointInfo);
        if(!CollectionUtils.isEmpty(xuYiPointInfos)){
            xuYiPointInfo = xuYiPointInfos.get(0);
        }
        return xuYiPointInfo;
    }

    public int pointInfoModify(String pointId,String pointName, String gpsLngLats,String remark,String operatorTel) {

        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();

        String[] gpsLngLatArr = gpsLngLats.split("\\|");
        String gpsLng;
        String gpsLat;
        String bdLng = "";
        String bdLat = "";
        String bdLngLats = "";
        int insertCheckResult =0;
        try{
            if(null!=gpsLngLatArr&&gpsLngLatArr.length>0){
                for(String gpsLngLat : gpsLngLatArr){
                    gpsLng = gpsLngLat.split(",")[0];
                    gpsLat = gpsLngLat.split(",")[1];
                    String lngLat = SmsInterfacesService.GPSToBd(gpsLng, gpsLat);

                    bdLng = lngLat.split(",")[0];
                    bdLat = lngLat.split(",")[1];
                    if(StringUtil.isNull(bdLngLats)){
                        bdLngLats = bdLng + "," + bdLat;
                    }else{
                        bdLngLats = bdLngLats + "|" +bdLng + "," + bdLat;
                    }
                }

                //入审核表
                xuYiCheckInfoBo.setPointId(pointId);
                xuYiCheckInfoBo.setPointName(pointName);
                xuYiCheckInfoBo.setGpsItdu(gpsLngLats);
                xuYiCheckInfoBo.setBdItdu(bdLngLats);
                xuYiCheckInfoBo.setRemark(remark);
                xuYiCheckInfoBo.setCheckState("0");
                xuYiCheckInfoBo.setType("2");
                xuYiCheckInfoBo.setOperator(operatorTel);
                insertCheckResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);

                //直接入表
//                xuYiPointInfo.setId(pointId);
//                xuYiPointInfo.setName(pointName);
//                xuYiPointInfo.setGpsLngLats(gpsLngLats);
//                xuYiPointInfo.setBdLngLats(bdLngLats);
//                xuYiPointInfo.setRemark(remark);
//                xuYiPointInfo.setOperatorTel(operatorTel);
//                insertCheckResult = xuYiPointMapper.modifyPointInfo(xuYiPointInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("新增点位信息发生异常:"+e.toString());
        }

        return  insertCheckResult;
    }


    public int deletePointInfo(String pointId,String pointName,String operatorTel) {
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        int insertCheckResult = 0;
        try {
            xuYiCheckInfoBo.setPointId(pointId);
            xuYiCheckInfoBo.setPointName(pointName);
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setType("3");
            xuYiCheckInfoBo.setOperator(operatorTel);
            insertCheckResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);
        }catch (Exception e){
            e.printStackTrace();
            log.info("删除点位信息发生异常:"+e.toString());
        }
        return  insertCheckResult;
    }
}
